package com.bxd.baodan.util.observer;

import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SmsContent extends ContentObserver {

	public static final String SMS_URI_INBOX = "content://sms/inbox";
	private Activity activity = null;
	private EditText verifyText = null;

	public SmsContent(Activity activity, Handler handler, EditText verifyText) {
		super(handler);
		this.activity = activity;
		this.verifyText = verifyText;
	}

	@Override
	public void onChange(boolean selfChange) {
		super.onChange(selfChange);
		Cursor cursor = null;// 光标
		String smsMain = "";
		// 读取收件箱中指定号码的短信
		cursor = activity.managedQuery(Uri.parse(SMS_URI_INBOX), new String[] {
				"_id", "address", "body", "read" }, null ,
				null, "date desc");
		if (cursor != null) {// 如果短信为未读模式
			cursor.moveToFirst();
			if (cursor.moveToFirst()) {
				String smsbody = cursor
						.getString(cursor.getColumnIndex("body"));
                // FIXME: 17/7/25 可能有坑  短信问题
                if(smsbody.contains("保险岛") ){
					String regex = "\\d{6}";
					Pattern p = Pattern.compile(regex);
					Matcher m = p.matcher(smsbody);
					while (m.find()) {
					if (!"".equals(m.group()))
						smsMain =  m.group();
					}
				}
				verifyText.setText(smsMain);
			}
		}
//		cursor.close();
	}
}